//
//  MyCenterViewController.m
//  WZCLApp
//
//  Created by chirui on 2018/7/2.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "MyCenterViewController.h"
#import "ChangeUserMessageVC.h"
#import "MySettingViewController.h"
#import "MyTicketsListViewController.h"
#import "BuyClassListViewController.h"  // 购课

@interface MyCenterViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{
    
    UITableView * _tableView;
    NSDictionary * userDic;
    UIButton * userIconBtn;
}

@end

@implementation MyCenterViewController
-(void)viewWillDisappear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden =NO;
    
}

-(void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden = YES; //隐藏
    self.tabBarController.tabBar.hidden = NO;
    
    if (verifTokenAndEnter) {
        
        //        [self getUserMessageRequest];
        //        [self getMessageCountRequest];
        //        [self getOrderCountRequest];
    }else {
        
        userDic = nil;
        
    }
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self creatTableView];
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, -SH_StatusBarHeight, WIDTH, HEIGHT-SH_StatusBarHeight+SH_TabBarHeight) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    //    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //
    //
    //    }];
    //    _tableView.mj_header = header;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 260;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId = @"cellId";
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }else {
        while ([cell.contentView.subviews lastObject]!=nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    NSArray * array =@[@"分享二维码",@"我的消息",@"关注班级",@"优惠券",@"设置"];
    NSArray * imageArray =@[@"icon_user_share",@"icon_user_news",@"icon_user_class",@"icon_user_coupon",@"icon_user_set"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = array[indexPath.row];
    cell.imageView.image = ImageName(imageArray[indexPath.row]);
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(10, 54.5, WIDTH -20,0.5)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [cell.contentView addSubview:view];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section==0) {
        
        UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        heardView.backgroundColor = [UIColor whiteColor];
        
        UIImageView * bgImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 245)];
        bgImage.userInteractionEnabled = YES;
        bgImage.image = ImageName(@"img_user_bg");
        [heardView addSubview:bgImage];
        
        userIconBtn = [self btnFrame:CGRectMake(10, (100+SH_TopHeight)/2-10, 60, 60) andImageName:@"img_head_portrait_pre" andSEL:@selector(photoClick) onView:bgImage];
        
        [userIconBtn sd_setImageWithURL:ImageUrl(USER_DEFAULTGet(@"avatar")) forState:UIControlStateNormal placeholderImage:ImageName(@"img_user_information_hp")];
        userIconBtn.layer.cornerRadius = 30.f;
        userIconBtn.layer.masksToBounds = YES;
        
        UILabel * userNameLabel =[[UILabel alloc]initWithFrame:CGRectMake(80, (100+SH_TopHeight)/2+10, 200, 22)];
        userNameLabel.font =[UIFont systemFontOfSize:16];
        userNameLabel.textColor =[UIColor whiteColor];
        userNameLabel.text = @"2233232323";
        userNameLabel.numberOfLines = 1;
        [userNameLabel sizeToFit];
        [bgImage addSubview:userNameLabel];
        
        UILabel * titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(100, 30, WIDTH -200, 20)];
        titleLabel.font =[UIFont boldSystemFontOfSize:17];
        titleLabel.textColor =[UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"我的";
        [bgImage addSubview:titleLabel];
        
        
        UIButton * editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        editBtn.frame = CGRectMake(WIDTH - 120, (100+SH_TopHeight)/2+8, 120, 28);
        [editBtn setBackgroundImage:[UIImage imageNamed:@"icon_user_button_bg"] forState:UIControlStateNormal];
        [editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [bgImage addSubview:editBtn];
        
        
        UIImageView * orderImage = [[UIImageView alloc]initWithFrame:CGRectMake(6, 150, WIDTH -12, 110)];
        orderImage.image = ImageName(@"img_user_list_bg");
        orderImage.userInteractionEnabled = YES;
        [heardView addSubview:orderImage];
        
        float width  = (WIDTH-12)/5;
        
        NSArray * twoArray = @[@"icon_order_class_list",@"icon_user_order_payment",@"icon_user_order_pd",@"icon_user_orderok",@"icon_user_order_refund"];
        NSArray * twoTitleArray = @[@"购课单",@"待付款",@"已付款",@"已完成",@"退款"];
        
        UIView *  twoView;
        for (int j = 0; j<5; j++) {
            
            twoView =[[UIView alloc]initWithFrame:CGRectMake(0 + width * j, 0, width, 90)];
            twoView.backgroundColor =[UIColor clearColor];
            [twoView tapAction:^{
                
                if (j==0) {
                    
                    
                    BuyClassListViewController * class = [[BuyClassListViewController alloc]init];
                    class.hidesBottomBarWhenPushed = YES;
                     self.navigationController.navigationBar.translucent = NO;
                    [self.navigationController pushViewController:class animated:YES];
                }
                
            }];
            
            UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake((width - 40)/2, 10, 40, 40)];
            imageView.image = ImageName(twoArray[j]);
            [twoView addSubview:imageView];
            
            UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+5, width, 20)];
            label.font = [UIFont systemFontOfSize:14];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = RGBa(0x666666);
            label.text = twoTitleArray[j];
            [twoView addSubview:label];
            
            [orderImage addSubview:twoView];
        }
        
        
        if (userDic) {
            [userIconBtn sd_setBackgroundImageWithURL:ImageUrl(userDic[@"avatar"]) forState:UIControlStateNormal placeholderImage:ImageName(@"img_head_portrait_pre")] ;
            userNameLabel.text = userDic[@"user_name"];
            userNameLabel.numberOfLines = 1;
            [userNameLabel sizeToFit];
        }
        
        return heardView;
        
    }else {
        
        return nil;
    }
}

-(void)editBtnClick{
    
    ChangeUserMessageVC * change = [[ChangeUserMessageVC alloc]init];
    change.hidesBottomBarWhenPushed= YES;
    [self.navigationController pushViewController:change animated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0) {
        
    }else if (indexPath.row==3){
        
        MyTicketsListViewController *  tickets = [[MyTicketsListViewController alloc]init];
        tickets.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController pushViewController:tickets animated:YES];
        
    }else if (indexPath.row==4){
        
        MySettingViewController * set = [[MySettingViewController alloc]init];
        set.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController pushViewController:set animated:YES];
    }else {
        
        
    }
    
}



-(void)photoClick{
    
    
}

-(UIButton *)btnFrame:(CGRect)rec andImageName:(NSString *)imageName andSEL:(SEL)sel onView:(UIView *)view{
    
    UIButton * scanBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    scanBtn.frame = rec;
    [scanBtn setBackgroundImage:ImageName(imageName) forState:UIControlStateNormal];
    [scanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [scanBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    scanBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [view addSubview:scanBtn];
    return scanBtn;
    
}



@end

